package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NCException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SmsMapper;
import com.nineclock.system.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
@Transactional
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    SmsMapper smsMapper;
    @Autowired
    AliyunSmsUtils aliyunSmsUtils;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Override
    public void sendSms(String mobile, String type) {
        // 对手机号进行校验
        boolean isLegal = PhoneNumCheckUtils.isChinaPhoneLegal(mobile);
        if (mobile == null || !isLegal) {
            throw new NCException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        // 根据手机号查询相关记录
        Integer count = smsMapper.selectCountByMobile(mobile);
        // 再判断验证码的类型是什么
        if (SMSConstant.SMS_TYPE_REGISTER.equals(type)) {
            // 是注册,手机号必须不存在,存在则不能注册
            if (count > 1) {
                throw new NCException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        } else {
            // 说明不是注册,手机号必须存在且唯一
            if (count != 1) {
                throw new NCException(ResponseEnum.USER_NOT_REGISTER);
            }
        }
        // 生成随机的验证码
        String randomParam = RandomUtil.randomNumbers(6);
        Boolean isSuccess = true;//aliyunSmsUtils.sendSMS(mobile, randomParam);
        // 判断是否发送成功,发送成功,把验证码存redis中,
        if (!isSuccess) {
            throw new NCException(ResponseEnum.CODE_IMAGE_ERROR);
        }
        // 将验证码存到redis中,并设置有效期
        String key = getPrefix(type) + mobile;
        redisTemplate.opsForValue().set(key, randomParam, 5, TimeUnit.MINUTES);
  /*      String key = getPrefix(type) + mobile;
        redisTemplate.boundValueOps(key).set(randomParam, 5, TimeUnit.MINUTES);*/
        log.info("发送成功后的手机号" + key);
        log.info("发送成功后的验证码:" + randomParam);

    }

    // 校验验证码
    @Override
    public Boolean verify(String checkCode, String type, String mobile) {
        if (checkCode == null || mobile == null) {
            throw new NCException(ResponseEnum.MESSAGE_PARAMS_LOST);
        }
        String prefix = getPrefix(type);
        // 从redis中取出数据
        String newCode = redisTemplate.opsForValue().get(prefix + mobile);
        if (!checkCode.equals(newCode)) {
            return false;
        }
        return true;
    }

    private String getPrefix(String type){
        String prefix = "";
        switch (type){
            case "1":
                prefix = SMSConstant.SMS_LOGIN_KEY_PREFIX;//登录
                break;
            case "2":
                prefix = SMSConstant.SMS_REGISTER_KEY_PREFIX;//注册
                break;
            case "3":
                prefix = SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;//更改管理员
                break;
            case "4":
                prefix = SMSConstant.SMS_RESET_KEY_PREFIX;//重置密码
                break;
            default:
                break;
        }
        return prefix;
    }
}
